package com.avic.rong.course.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.avic.rong.R;
import com.avic.rong.course.activity.PeiXunVideoActivity;
import com.avic.rong.course.bean.PeiXunListBean;
import com.avic.rong.course.bean.PeiXunSortBean;
import com.avic.rong.course.showapi.PeiXunListApi;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.ToastTools;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import java.util.List;

/**
 * Created by Chenjiaxin on 2019/8/11 18:04
 * E-Mail Address：16601203965@163.com
 */
public class PeiXunVideoVpAdapter extends PagerAdapter {

    private Context context;
    private List<PeiXunSortBean.ClassifyListBean> titleList;
    private LayoutInflater inflater;
    private PeiXunVideoRvAdapter rvAdapter;
    private RecyclerView rv;

    public PeiXunVideoVpAdapter(Context context,List<PeiXunSortBean.ClassifyListBean> titleList) {
        this.context = context;
        this.titleList = titleList;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return titleList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return titleList.get(position).getName();
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = inflater.inflate(R.layout.view_peixun_rv,container,false);

        rv = view.findViewById(R.id.peixun_video_rv);

        int id = titleList.get(position).getId();
        StringBuffer sb = new StringBuffer();
        sb.append(id+":0");
        initListData(sb.toString());

        container.addView(view);
        return view;
    }

    private void initListData(String classifyName) {
        PeiXunListApi classMuLuApi = new PeiXunListApi((PeiXunVideoActivity)context, simpleOnNextListener, 10+"",classifyName,10,"zh:");
        classMuLuApi.setPageNo(1);
        classMuLuApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(classMuLuApi);
    }
    //提交报名咨询数据监听
    HttpOnNextListener simpleOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(context, subjects.getMsg());
                Log.e("cjx", subjects.getMsg());
                return;
            }
            PeiXunListBean bean = (PeiXunListBean) subjects.getResult();

            rv.setLayoutManager(new LinearLayoutManager(context));
            rvAdapter = new PeiXunVideoRvAdapter(context);
            rv.setAdapter(rvAdapter);
        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "培训列表数据：" + e.getMessage());
        }
    };


    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}
